<?php

namespace App\Store\Models\OldMaterial;

use App\Common\Services\Common;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Basic\GoodsCraft;
use App\Store\Models\Basic\GoodsPricetype;
use App\Store\Models\Basic\GoodsStone;
use App\Store\Models\Basic\GoodsStyle;
use App\Store\Models\Basic\PaytypeList;
use App\Store\Models\Basic\Supply;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Log extends Model
{
    protected $table = 'site_oldmaterial_log';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request)
    {

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $store_ids = $accountinfo['store_ids']?? [];
        $query = self::with(['storeinfo', 'startstoreinfo', 'targetstoreinfo', 'goods_category'])->where('cp_id', $cp_id);


        // 回收时间
        $start_time = $request->start_time;
        $end_time = $request->end_time;
        if ($start_time && $end_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                // $query->whereIn('store_id',$store_ids);
                $query->where(function ($q) use ($store_ids){
                    $q->whereIn('start_store_id', $store_ids)->orWhereIn('target_store_id', $store_ids);
                });
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表

                $query->where(function ($q) use ($store_ids){
                    $q->where('start_store_id', intval($store_ids))->orWhere('target_store_id', intval($store_ids));
                });
            }
        }

        // 所属门店
        $store_id = $request->store_id ?? 0;
        if ($store_id > 0) {
            $query->where(function ($q) use ($store_id){
                $q->where('start_store_id', $store_id)->orWhere('target_store_id', $store_id);
            });
        }

        //ids
        $ids = $request->ids;
        $ids = is_array($ids) ? $ids : (is_array(json_decode($ids, true)) ? json_decode($ids, true) : []);
        if ($ids != []) {
            $query->whereIn('id', $ids);
        }

        // 回收方式
        $stock_type = $request->stock_type ?? 0;
        if ($stock_type > 0) {
            $query->where('stock_type', $stock_type);
        }

        // 关联单号
        $order_sncode = $request->order_sncode ?? '';
        if ($order_sncode != '') {
            $query->where('order_sncode', 'like', '%' . $order_sncode . '%');
        }

        // 旧料类别
        $goods_category_id = $request->goods_category_id ?? 0;
        if ($goods_category_id > 0) {
            $query->where('goods_category_id', $goods_category_id);
        }

        // 日志类型
        $log_type = $request->log_type ?? 0;
        if ($log_type > 0) {
            $query->where('log_type', $log_type);
        }

        // 证书号
        $cf_no = $request->cf_no ?? '';
        if ($cf_no != '') {
            $query->where('cf_no', 'like', '%' . $cf_no . '%');
        }

        $isgetall = $request->isgetall ?? 0;

        $query->orderBy('id','desc');

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        }

        $list = $query->get()->toArray();
        return ['list' => $list];
    }

    /**
     * 关联类别
     */
    public function goods_category(){
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id')->select('id','category_name');
    }

    /**
     * 关联款式
     */
    public function goods_style(){
        return $this->hasOne(GoodsStyle::class, 'id', 'goods_style_id')->select('id','style_name');
    }

    /**
     * 店铺
     */
    public function storeinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id', 'store_name');
    }

    public function startstoreinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'start_store_id')->select('id', 'store_name');
    }

    public function targetstoreinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'target_store_id')->select('id', 'store_name');
    }

    public function supply(){
        return $this->hasOne(Supply::class, 'id', 'supply_id')->select('id','supply_name');
    }

    public function goods_craft(){
        return $this->hasOne(GoodsCraft::class, 'id', 'goods_craft_id')->select('id','craft_name');
    }
    public function goods_pricetype(){
        return $this->hasOne(GoodsPricetype::class, 'id', 'goods_pricetype_id')->select('id','pricetype_name');
    }
    public function goods_stone(){
        return $this->hasOne(GoodsStone::class, 'id', 'goods_stone_id')->select('id','stone_name');
    }
    public function paytype_info(){
        return $this->hasOne(PaytypeList::class, 'id', 'goods_stone_id')->select('id','paytype_name');
    }


    /**
     * 添加日志
     * @param $param
     * @param $log_type
     * @return void
     */
    public function addLog($param,$log_type=0)
    {
        $data=[
            'cp_id' => $param['cp_id'],
            'store_id' => $param['store_id'],
            'start_store_id' => $param['start_store_id'],
            'target_store_id' => $param['target_store_id'],
            'order_sncode' => $param['order_sncode'],
            'goods_category_id' => $param['goods_category_id'],
            'piece_weight' => $param['piece_weight'],
            'stock_type' => $param['stock_type'],
            'count' => $param['count'],
            'cf_no' => $param['cf_no'],
            'log_type' => $log_type,
            'create_account_id' => $param['create_account_id']??0,
            'create_account_real_name' => $param['create_account_real_name']??'',
            'create_at' =>$param['create_at']?? time(),
        ];

        if(!empty($data)){
            Log::query()->create($data);
        }

    }
}

